﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using JPGResizer.Model;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Media;

namespace JPGResizer.Helper
{
    /// <summary>
    /// 数据
    /// </summary>
    class ValueHelper
    {
        #region 当前所处理的数据集合
        private static ValueHelper _CurrentValues = null;
        /// <summary>
        /// 当前所处理的数据集合
        /// </summary>
        public static ValueHelper CurrentValues
        {
            get
            {
                if (_CurrentValues == null)
                {
                    _CurrentValues = new ValueHelper();
                }
                return _CurrentValues;
            }
        } 
        #endregion

        #region Constructor
        private ValueHelper()
        {
            this._currentNum = this.StartNum;
            //
            this.ResizeType = Helper.ResizeType.ResizeType1;
            this.NameType = Helper.NameType.NameType1;
            this.Images = new ObservableCollection<MyImage>();
            this.ResizePercent = 100;
            this.Length = 100;
            this.StartNum = 0;
            this.FirstExtenssion = string.Empty;
            this.LastExtenssion = "_s";
            this.SavePath = @"C:\Images";
        } 
        #endregion

        /// <summary>
        /// 保存时所计算的当前数
        /// </summary>
        private int _currentNum;
        /// <summary>
        /// jpg文件的后缀
        /// </summary>
        public const string JPEGExtenssion = "jpg";

        #region Events

        /// <summary>
        /// 保存图片的进度变化后触发
        /// 仅仅能挂载一次
        /// </summary>
        public ProgressChangedEventHander SaveImagesProgressChanged;
        private void OnSaveImagesProgressChanged(double value)
        {
            if (SaveImagesProgressChanged != null)
            {
                SaveImagesProgressChanged(value);
            }
        }

        #endregion

        #region public methods

        /// <summary>
        /// 消除当前循环的存储
        /// 1.图片列表
        /// 2.所计算的图片数
        /// </summary>
        public void ClearCache()
        {
            this.Images = new ObservableCollection<MyImage>();
            this._currentNum = this.StartNum;
        }

        /// <summary>
        /// 从完整路径中获取文件名
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetFilenameFromFullPath(string path)
        {
            var strs = path.Split('\\');
            if (strs.Length > 0)
            {
                return strs[strs.Length - 1];
            }
            return path;
        }

        /// <summary>
        /// 转换文件名
        /// </summary>
        /// <param name="originalFilename"></param>
        /// <returns></returns>
        public string ConvertFilename(string originalFilename)
        {
            string convertedFilename = originalFilename;
            //
            string name = string.Empty;
            string extenssion = string.Empty;
            var strs = originalFilename.Split('.');
            if (strs.Length > 0)
            {
                extenssion = strs[strs.Length - 1];
                name = originalFilename.Replace("." + extenssion, "");
            }
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(extenssion))
            {
                switch (this.NameType)
                {
                    case Helper.NameType.NameType1:
                        {
                            convertedFilename = this.FirstExtenssion + name + this.LastExtenssion + "." + JPEGExtenssion;//+ extenssion;
                            break;
                        }
                    case Helper.NameType.NameType2:
                        {
                            convertedFilename = this.FirstExtenssion + this._currentNum + this.LastExtenssion + "." + JPEGExtenssion;// + extenssion;
                            break;
                        }
                }
            }
            //
            return convertedFilename;
        }

        /// <summary>
        /// 缩放并保存图片
        /// </summary>
        /// <param name="img">图片</param>
        /// <param name="scare">图片的变形</param>
        public void Scale_SaveImage(MyImage img, ScaleTransform scare)
        {
            if (img != null && scare != null)
            {
                ////缩放
                var scaredBitMap = new TransformedBitmap(img.ImageSource, scare);
                try
                {
                    string path = this.SavePath + img.Filepath;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    using (var stream = File.Create(path + "\\" + ConvertFilename(img.Filename)))
                    {
                        var encoder = new JpegBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(scaredBitMap));
                        encoder.Save(stream);
                    }
                }
                catch (Exception ex)
                {
                    Helper.ErrorHelper.ErrorHandler(ex);
                }
                finally
                {
                    this._currentNum++;
                    this.OnSaveImagesProgressChanged((double)(this._currentNum - this.StartNum) / (double)this.Images.Count);
                }
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// 缩放类型
        /// </summary>
        public ResizeType ResizeType { get; set; }
        /// <summary>
        /// 命名规则
        /// </summary>
        public NameType NameType { get; set; }
        /// <summary>
        /// 当前要处理的图
        /// </summary>
        public ObservableCollection<MyImage> Images { get; set; }
        /// <summary>
        /// 缩放的比例(百分比多少)
        /// 默认为一百
        /// </summary>
        public double ResizePercent { get; set; }
        /// <summary>
        /// 缩放后的宽度/长度
        /// </summary>
        public int Length { get; set; }
        /// <summary>
        /// 起始数字
        /// </summary>
        public int StartNum { get; set; }
        /// <summary>
        /// 前缀
        /// </summary>
        public string FirstExtenssion { get; set; }
        /// <summary>
        /// 附缀
        /// </summary>
        public string LastExtenssion { get; set; }
        /// <summary>
        /// 最终文件的保存路径
        /// </summary>
        public string SavePath { get; set; } 

        #endregion
    }
}
